package com.estate.web.controller.system;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.estate.common.annotation.Log;
import com.estate.common.constant.UserConstants;
import com.estate.common.core.controller.BaseController;
import com.estate.common.core.domain.AjaxResult;
import com.estate.common.core.domain.entity.SysMenu;
import com.estate.common.core.domain.req.SysMenuAddReq;
import com.estate.common.core.domain.req.SysMenuEditReq;
import com.estate.common.core.domain.resp.SysMenuResp;
import com.estate.common.core.page.TableDataInfo;
import com.estate.common.enums.BusinessType;
import com.estate.common.utils.StringUtils;
import com.estate.common.utils.bean.BeanUtils;
import com.estate.system.service.ISysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 菜单信息
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/menu")
public class SysMenuController extends BaseController
{
    @Autowired
    private ISysMenuService menuService;

    /**
     * 获取菜单列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysMenu menu)
    {
        startPage();
        Page<SysMenu> menus = menuService.selectMenuPage(menu);
        return getDataTable(menus, SysMenuResp.class);
    }

    /**
     * 根据菜单编号获取详细信息
     */
    @GetMapping(value = "/{menuId}")
    public AjaxResult getInfo(@PathVariable Long menuId)
    {
        return success(menuService.selectMenuById(menuId), SysMenuResp.class);
    }

    /**
     * 新增菜单
     */
    @Log(title = "Menu Management", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysMenuAddReq req)
    {
        SysMenu menu = new SysMenu();
        BeanUtils.copyBeanProp(menu, req);
        menu.setParentId(0L);
        if (!menuService.checkMenuNameUnique(menu))
        {
            return error("Failed to add menu " + menu.getMenuName() + ", menu name already exists");
        }
        else if (UserConstants.YES_FRAME.equals(menu.getIsFrame()) && !StringUtils.ishttp(menu.getPath()))
        {
            return error("Failed to add menu " + menu.getMenuName() + ", URL must start with http(s)://");
        }
        menu.setCreateBy(getUsername());
        return toAjax(menuService.insertMenu(menu));
    }

    /**
     * 修改菜单
     */
    @Log(title = "Menu Management", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysMenuEditReq req)
    {
        SysMenu menu = new SysMenu();
        BeanUtils.copyBeanProp(menu, req);
        menu.setParentId(0L);
        if (!menuService.checkMenuNameUnique(menu))
        {
            return error("Failed to update menu " + menu.getMenuName() + ", menu name already exists");
        }
        else if (UserConstants.YES_FRAME.equals(menu.getIsFrame()) && !StringUtils.ishttp(menu.getPath()))
        {
            return error("Failed to update menu " + menu.getMenuName() + ", URL must start with http(s)://");
        }
        else if (menu.getMenuId().equals(menu.getParentId()))
        {
            return error("Failed to update menu " + menu.getMenuName() + ", parent menu cannot be itself");
        }
        menu.setUpdateBy(getUsername());
        return toAjax(menuService.updateMenu(menu));
    }

    /**
     * 删除菜单
     */
    @Log(title = "Menu Management", businessType = BusinessType.DELETE)
    @DeleteMapping("/{menuId}")
    public AjaxResult remove(@PathVariable("menuId") Long menuId)
    {
        if (menuService.hasChildByMenuId(menuId))
        {
            return warn("Submenus exist, deletion is not allowed");
        }
//        if (menuService.checkMenuExistRole(menuId))
//        {
//            return warn("Menu is assigned, deletion is not allowed");
//        }
        return toAjax(menuService.deleteMenuById(menuId));
    }
}